package com.jrx.anytxn.transaction.job.autoPayment.config;


import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.entity.BtAutoPaymentList;
import com.jrx.anytxn.transaction.job.autoPayment.bean.PaymentLogBean;
import com.jrx.anytxn.transaction.job.autoPayment.step.AutoPaymentPreProcessor;
import com.jrx.anytxn.transaction.job.autoPayment.step.AutoPaymentPreReader;
import com.jrx.anytxn.transaction.job.autoPayment.step.AutoPaymentPreWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;

/**
 * 扣款批扣准备
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class AutoPaymentPreConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private AutoPaymentPreProcessor autoPaymentPreProcessor;

    @Autowired
    private AutoPaymentPreWriter autoPaymentPreWriter;


    @Bean
    public Job autoPaymentPreJob(){
        return jobBuilderFactory.get("autoPaymentPreJob")
                .start(autoPaymentPreStep())
                .build();
    }

    @Bean
    public Step autoPaymentPreStep(){
        return stepBuilderFactory.get("autoPaymentPreStep")
                .<BtAutoPaymentList, PaymentLogBean>chunk(anytxnBatchProperties.getAutoPaymentConfiguration().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(autoPaymentReadyPreReader())
                .processor(autoPaymentPreProcessor)
                .writer(autoPaymentPreWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getAutoPaymentConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<BtAutoPaymentList> autoPaymentReadyPreReader() {
        return new AutoPaymentPreReader(sqlSessionFactory, anytxnBatchProperties.getAutoPaymentConfiguration().getPageSize());
    }
}
